﻿using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBot : Actor, IDamage
{

    protected override void Awake()
    {
        base.Awake();
        BindActorInfo(new ActorInfo(1,"Bot",2,1,1));
    }
    Vector3 oldPos;
    void Update()
    {
        Move();

    }
    void Move()
    {
        if ((transform.position - oldPos).magnitude > 0)
        {
            animator.SetBool("Fly", true);
        }
        if ((transform.position - oldPos).x > 0)
        {
            spriteRenderer.flipX = true;
        }
        else if ((transform.position - oldPos).x < 0)
        {
            spriteRenderer.flipX = false;
        }
        oldPos = transform.position;
    }

    public void OnDamage(GameObject sender, Vector3 pos, int Attack)
    {
        if (isDeath)
        {
            return;
        }
        actorInfo.TakeHp(Attack);
        if (actorInfo.hp <= 0)
        {
            sender.GetComponent<Character>().AddCoin(2);
            ToDeath();
            return;
        }
        animator.SetTrigger("Hit");
        Vector2 dir = transform.position - pos;
        dir.Normalize();
        GameObjectPool.instance.CreateObject("HitEffect2", Resources.Load<GameObject>("Prefabs/Effect/HitEffect2"), transform.position, Quaternion.identity);
        AudioManager.Instance.PlayAudio(Resources.Load<AudioClip>("Audio/enemy_damage"));

        StartCoroutine(ToRepel(dir, 0.5f));
    }

    private void ToDeath()
    {
        Destroy(gameObject,0.5f);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag=="Player")
        {
            var dam = collision.GetComponent<IDamage>();
            dam.OnDamage(gameObject,transform.position,actorInfo.attack);
        }
    }
}
